package com.icebergteam.warfacewidget.data.sql;

import java.io.File;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.utils.ViewUtils;

public class WikiTable extends BasicTable {
	public static String className = WikiTable.class.getCanonicalName();
	// Constants
	// Table
	public static final String TABLE_NAME	= "wiki";
	// Fields
	public static final String FIELD_ID				= "id ";
	public static final String FIELD_URL			= "url ";
	public static final String FIELD_HTML			= "html ";
	public static final String FIELD_TITLE = "title ";

	

	public WikiTable() {
		
	}
	
	public String createTableStr() {
		LogManager.LogV(className, "createTable");
		String field_id			= FIELD_ID		+ "integer primary key autoincrement not null";
		String field_url		= FIELD_URL		+ "text";
		String field_html		= FIELD_HTML	+ "text";
		String field_title 		= FIELD_TITLE   + "text";
			
	
		return AppManager.getDatabase().createTableStr(TABLE_NAME, field_id, field_url, field_html, field_title);
	
	}
	
	
		
	public Cursor getWikiHtmlByUrl(String url) throws SQLException{
		try {
			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
			if(db != null && db.isOpen()){
				return db.query(
					WikiTable.TABLE_NAME, 
					new String[] {WikiTable.FIELD_ID, WikiTable.FIELD_TITLE, WikiTable.FIELD_URL, WikiTable.FIELD_HTML}, 
					String.format("%s = ?", WikiTable.FIELD_URL), 
					new String[]{url}, 
					null, 
					null,
					null
					);
			}
		} catch (Exception e) {
			LogManager.LogEA(WikiTable.className, ViewUtils.getErrorMessage(e), url);
			
		}
		return null;
	}
	
	public Cursor getWikiHtmlByInd(int id) throws SQLException{
		
		try {
			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
			if(db != null && db.isOpen()){
				return db.query(
					WikiTable.TABLE_NAME, 
					new String[] {WikiTable.FIELD_ID, WikiTable.FIELD_TITLE, WikiTable.FIELD_URL, WikiTable.FIELD_HTML}, 
					String.format("%s = ?", WikiTable.FIELD_ID), 
					new String[]{String.format("%s", id)}, 
					null, 
					null,
					null
					);
			}
		} catch (Exception e) {
			LogManager.LogEA(WikiTable.className, ViewUtils.getErrorMessage(e));
		}
		return null;
	}
	
	public void removeWikiHtml(String url) throws SQLException{
		SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
		if(db != null && db.isOpen()){
					
			Cursor cursor = db.rawQuery(String.format("select %s from %s where %s = '%s'", CacheTable.FIELD_FILE_URI, CacheTable.TABLE_NAME, CacheTable.FIELD_PARENT, url),null);
					
			while(cursor!= null && cursor.moveToNext()){
				String file_uri = cursor.getString(cursor.getColumnIndex(CacheTable.FIELD_FILE_URI.trim()));
				
				Cursor countFilesCursor = db.rawQuery(String.format("select count(*) from %s where %s = '%s'", CacheTable.TABLE_NAME, CacheTable.FIELD_FILE_URI, file_uri), null);
				countFilesCursor.moveToFirst();
				int countFiles = countFilesCursor.getInt(0);
				
				if(countFiles == 1 ){
					ViewUtils.deleteFile(file_uri);
				}
				
				db.delete(CacheTable.TABLE_NAME, String.format("%s = '%s' AND %s = '%s'",CacheTable.FIELD_PARENT, url, CacheTable.FIELD_FILE_URI, file_uri), null); 
			
			}
		
			db.delete(WikiTable.TABLE_NAME, String.format("%s = '%s'",WikiTable.FIELD_URL, url), null);
		}
	}
	
	public void deleteAll() throws SQLException{
		SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
		if(db != null && db.isOpen()){
			db.delete(WikiTable.TABLE_NAME, null, null);
			db.delete(CacheTable.TABLE_NAME, String.format("%s = '%s'", CacheTable.FIELD_TYPE_PARENT, WikiTable.TABLE_NAME), null);

		}
		ViewUtils.deleteRecursive(new File(WidgetProvider.CACHE_PATH));
	}
	
	@Override
	protected WhereWrapper getUpdateWhere(ContentValues cv) {
		WhereWrapper where = new WhereWrapper();
		where.setWhere( String.format("%s=?", WikiTable.FIELD_URL));
		where.addValue(cv.getAsString(WikiTable.FIELD_URL));
		
		return where;
	}
	
	@Override
	protected String getTableName() {
		return TABLE_NAME;
	}
}